package com.ruoyi.common.core.utils;

import com.ruoyi.common.core.exception.ServiceException;

/**
 * 类描述:
 *
 * @author 31592
 * @date 2023-09-18 22:04
 */
public class MathUtils {

    private static String[]  functions = {"abs","sqrt","random","sin","cos","tan","asin","acos","atan","atan2","log10","exp","pow","max","min","round","ceil","floor"};

    public static String formateFormula(String formulaStr){
        for(String function : functions){
            formulaStr = formulaStr.replace(function,"Math." + function);
        }
        return formulaStr;
    }

    public static String getValueByUnit(String valueUnit) {
        return splitValueAndUnit(valueUnit)[0];
    }

    public static String[] splitValueAndUnit(String valueUnit) {
        int splitIndex = -1;
        for (int i = 0; i < valueUnit.length(); i++) {
            char c = valueUnit.charAt(i);
            if (!Character.isDigit(c) && c != '.') {
                splitIndex = i;
                break;
            }
        }
        if (splitIndex != -1) {
            String value = valueUnit.substring(0, splitIndex);
            String unitName = valueUnit.substring(splitIndex);
            return new String[]{value,unitName};
        }
        return new String[]{valueUnit};
    }

    public static void main(String[] args) {
        System.out.println(Math.log10(2));
    }
}
